package com.centaline.androidsalesblog.activities.sale;


import android.view.Menu;
import android.view.MenuItem;

import com.android.volley.VolleyError;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.api.sale.StoreListApi;
import com.centaline.androidsalesblog.application.MyLocation;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.base.BaseStoreListMapActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.fragments.list.StoreListFragment;
import com.centaline.androidsalesblog.fragments.listmap.StoreMapFragment;
import com.centaline.androidsalesblog.iml.RefreshListener;
import com.centaline.androidsalesblog.model.responseobject.StoreListRo;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.Locale;

/**
 * 附近门店列表
 * Created by Lihq on 2016/2/15.
 */
public class StoreListActivity extends BaseStoreListMapActivity implements BDLocationListener {

    private StoreListFragment storeListFragment;
    private StoreMapFragment storeMapFragment;

    private StoreListApi storeListApi;
    private LocationClient locationClient;

    public LatLng latLng;
    private double mLat, mLong;
    private boolean isFirst = true;
    private boolean mode;//true:列表;false:地图
    private boolean flag;


    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_store_list);
        super.findViews();
    }

    @Override
    protected void initViews() {
        super.initViews();
        locationClient = MyLocation.getLocationClient();
        locationClient.registerLocationListener(this);
        storeListApi = new StoreListApi(StoreListActivity.class, StoreListRo.class, new
                IResponseListener<StoreListRo>() {
                    @Override
                    public void onResponse(StoreListRo storeListRo) {
                        storeListFragment.stopRefresh(storeListRo.getList());
                        if (flag) {
                            if (storeListApi.getStartIndex() == 0 && !isFirst) {
                                snack(storeListRo.getTotal() == 0 ?
                                        getString(R.string.snack_store_nothing) :
                                        String.format(Locale.CHINA, getString(R.string.snack_store), storeListRo
                                                .getTotal()));
                            }
                        } else {
                            if (storeListApi.getStartIndex() == 0) {
                                snack(storeListRo.getTotal() == 0 ?
                                        getString(R.string.snack_store_nothing) :
                                        String.format(Locale.CHINA, getString(R.string.snack_store), storeListRo
                                                .getTotal()));
                            }
                            isFirst = false;
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        storeListFragment.refreshError();
                        netWorkUnable();
                    }
                });
    }

    @Override
    protected BaseFragment getFragment1() {
        storeListFragment = new StoreListFragment();
        storeListFragment.setRefreshListener(new RefreshListener() {
            @Override
            public void down() {
                storeListApi.setStartIndex(0);
                locationClient.start();
            }

            @Override
            public void up(int count) {
                storeListApi.setStartIndex(count);
                request();
            }
        });
        return storeListFragment;
    }


    @Override
    protected BaseFragment getFragment2() {
        storeMapFragment = new StoreMapFragment();
        return storeMapFragment;
    }

    private void request() {
        storeListApi.setPagecount(10);
        if (AppConstant.NearbyEnable) {
            storeListApi.setLat(String.valueOf(mLat));
            storeListApi.setLng(String.valueOf(mLong));
            storeListApi.setSort("4");
        } else {
            storeListApi.setSort("2");
        }
        storeListApi.startRequest();
    }


    private void reset() {
        storeListApi.setStartIndex(0);
        request();
    }

    @Override
    protected void onDestroy() {
        locationClient.unRegisterLocationListener(this);
        locationClient.stop();
        super.onDestroy();
    }

    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        locationClient.stop();
        final int errorCode = bdLocation.getLocType();
        switch (errorCode) {
            case 61:
            case 161:
                mLat = bdLocation.getLatitude();
                mLong = bdLocation.getLongitude();
                latLng = new LatLng(mLat, mLong);
                reset();
                break;
            default:
                reset();
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (AppConstant.NearbyEnable) {
            getMenuInflater().inflate(R.menu.menu_store_list_map, menu);
            MenuItem menuItem = menu.findItem(R.id.action_mode);
            menuItem.setIcon(mode ? R.drawable.ic_menu_map : R.drawable.ic_menu_list);
        } else {
            flag = false;
            viewPager.setCurrentItem(1, true);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (R.id.action_mode == item.getItemId()) {
            viewPager.setCurrentItem(mode ? 0 : 1, true);
            mode = !mode;
            invalidateOptionsMenu();
            baiduEvent(mode?"K1_3_Click":"K2_3_Click");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

